<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		Commercial
*/

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');
JLoader::register('JSNPluginHelper', JPATH_COMPONENT.'/helpers/jsnplugin.php');

class JSNPluginViewPlugin extends JView
{
	protected $form;
	protected $item;
	protected $state;

	/**
	 * Display the view
	 */
	public function display($tpl = null)
	{
		// Initialiase variables.
		$db = & JFactory::getDBO();
		$this->form		= $this->get('Form');
		$this->item		= $this->get('Item');
		$this->item->language = @explode(",",$this->item->language);
		$this->form->setValue('language','',$this->item->language);
		$this->state	= $this->get('State');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}
		// get event
		if($this->item->id)
		{
			$db->setQuery("select e.*,ed.id as ed_id, ed.data from #__jsnp_event e INNER JOIN #__jsnp_plugin p ON e.id_group = p.id_group LEFT JOIN #__jsnp_event_data ed ON e.id = ed.id_event WHERE p.id = ".$this->item->id);
			$this->events = $db->loadObjectList();
			$lw = array();
			if($this->item->language)
			{
				
				for($l=0;$l<count($this->item->language);$l++)
				{
					$lang = $this->item->language[$l];
					$lw[] = " language =".$db->Quote($lang);
				}
			}
			$db->setQuery("select l.* from #__jsnp_language_data l WHERE l.id_plugin = ".$this->item->id ." and(". @implode(" OR ",$lw).")");
			$languages = $db->loadObjectList();
			if($languages)
			{
				$newlangs = array();
				foreach($languages as $lang)
				{
					$newlangs[$lang->language] = $lang;
				}
				$this->languages = $newlangs;
			}

		}	
		$this->field_languages = JSNPluginHelper::getFieldLanguages($this->item->id);

		$this->addToolbar();
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addToolbar()
	{
		JRequest::setVar('hidemainmenu', true);

		$user		= JFactory::getUser();
		$userId		= $user->get('id');
		$isNew		= ($this->item->id == 0);
		$checkedOut	= false;//!($this->item->checked_out == 0 || $this->item->checked_out == $userId);
		// Since we don't track these assets at the item level, use the category id.
		$canDo		= JSNPluginHelper::getActions($this->item->id,0);

		JToolBarHelper::title($isNew ? JText::_('COM_JSNPLUGIN_PLUGIN_VIEW_HEADER_NEW') : JText::_('COM_JSNPLUGIN_PLUGIN_VIEW_HEADER_EDIT'));

		// If not checked out, can save the item.
		JToolBarHelper::apply('plugin.apply');
		JToolBarHelper::save('plugin.save');

		if ($canDo->get('core.create')) {
			JToolBarHelper::save2new('plugin.save2new');
		}
		/*
		// If an existing item, can save to a copy.
		if (!$isNew && $canDo->get('core.create')) {
			JToolBarHelper::save2copy('plugin.save2copy');
		}
		*/
		if (empty($this->item->id))  {
			JToolBarHelper::cancel('plugin.cancel');
		}
		else {
			JToolBarHelper::cancel('plugin.cancel', 'JTOOLBAR_CLOSE');
		}

	}
}
